package edu.nyupoly.wzj;

import java.util.Scanner;


public class Game {
	
	public static String getUserName() {
		String userName;
		System.out.println("Please input the user name:");
		
		Scanner inName = new Scanner(System.in);
		
		userName = inName.next();
		System.out.println();
		return userName;
		
	}
	
	/*
	 * Here this method "gameIsTie" is an easier way to check if the game is a tie.  O(1)
	 * You don't need to check if every cells are marked in this game board. O(n)
	 * n: total number of cells
	 */
	
	public static boolean gameIsTie(Player computer, Player human, Board gameBoard) {
		int totalNumOfCells = gameBoard.length*gameBoard.width;
		int totalAttempts = computer.attempt + human.attempt;
		
		//System.out.println("Total Attempts is :  "+ totalAttempts);
		if( totalAttempts == totalNumOfCells ) {
			return true;
		} else {
			return false;
		}
	}
	
	/*
	 * The human player starts to select his/her mark.
	 * 1 stands for CROSS, and 2 stands for NOUGHT
	 * guarantee the number is either 1 or 2.
	 */
	public static int getNumForMark() {
		
		int numForMark;
		
		Scanner inNum = new Scanner(System.in);
		try {
			System.out.println("Mark CROSS starts first!");
			System.out.println("Please select your mark, " +
					"1 stands for CROSS, and 2 stands for NOUGHT: ");
			numForMark = inNum.nextInt();
			
			if(numForMark!=1 && numForMark !=2) {
				System.out.println("Invalid Number! Please try it again! ");
				return getNumForMark();
			}
		} catch (Exception e) {
			System.out.println("Input Error! Please try it again! ");
			return getNumForMark();
		} 
		
		return numForMark;
	}
	
	/*
	 * To check if this game is over after human's attempt. 
	 * If yes, return true. Otherwise, return false.
	 */
	
	public static boolean humanToPlay(Player human,Player computer, Board gameBoard) {
			
		human.makeMove(gameBoard);
		human.increaseAttempt();
		gameBoard.showBoard();
		
		if(human.isWinner(gameBoard)){
			System.out.println(human.getName()+ " has won this game!!!");
			return true;
		} else {
			if(gameIsTie(human,computer,gameBoard)) { //if(gameBoard.isTie()) 
				System.out.println("This is a tie game");
				return true;
			} else {
				gameBoard.changePlayer();
				return false;
			}
		}
	}
	
	/*
	 * To check if this game is over after computer's attempt. 
	 * If yes, return true. Otherwise, return false.
	 */
	
	public static boolean computerToPlay(Player human, Player computer, Board gameBoard) {
		
		computer.makeMove(gameBoard);
		computer.increaseAttempt();
		gameBoard.showBoard();
		
		if(computer.isWinner(gameBoard)){
			System.out.println(computer.getName()+ " has won this game!!!");
			return true;
		} else {
			if(gameIsTie(human,computer,gameBoard)) {  //if(gameBoard.isTie()) {
				System.out.println("This is a tie game!!");
				return true;
			} else {
				gameBoard.changePlayer();
				return false;
			}
		}
	}
	
	public static String computerToPlay() {
		String userName;
		System.out.println("Please input the user name:");
		
		Scanner inName = new Scanner(System.in);
		
		userName = inName.next();
		System.out.println();
		return userName;
		
	}
	

	public static void main(String[] args) {
		
		System.out.println("The Game: Tic Tac Teo");
		
		String userName = getUserName();
		int numForMark = getNumForMark();
		
		Player computer,human;
		
		// The one whose mark is CROSS starts first.
		if(numForMark == 1) {
			computer = new ComputerPlayer("computer",Mark.NOUGHT);
			human = new HumanPlayer(userName,Mark.CROSS);
		} else {
			computer = new ComputerPlayer("computer",Mark.CROSS);
			human = new HumanPlayer(userName,Mark.NOUGHT);
		}
		
		computer.setOpponent(human);
		human.setOpponent(computer);
		
		System.out.println("Human Player's name is :  " + human.getName());
		System.out.println("Computer Player's name is :  " + computer.getName());
		
		//System.out.println(human.playerName);
		Board gameBoard = new Board(computer,human);
		
		//gameBoard.showBoard();
		
		//int totalAttempts = gameBoard.length*gameBoard.width;
		
		//int i = 0;
		
		//System.out.println("gameBoard.isFull() :  " + gameBoard.isFull());
			
		if(human.getMark() == Mark.CROSS){
			
			gameBoard.setCurrentPlayer(human);
			
			while(!gameIsTie(human,computer,gameBoard)) {
				boolean flag1 = humanToPlay(human, computer, gameBoard);
				if(flag1){
					return;
				}
				
				System.out.println();
				
				boolean flag2 = computerToPlay(human, computer, gameBoard);
				if(flag2){
					return;
				}
			}
				
		} else {
			
			gameBoard.setCurrentPlayer(computer);
				
			while(!gameIsTie(human,computer,gameBoard)) {
				
				boolean flag1 = computerToPlay(human, computer, gameBoard);
				if(flag1){
					return;
				}
				
				System.out.println();
				
				boolean flag2 = humanToPlay(human, computer, gameBoard);
				if(flag2){
					return;
				}
			}
		}
		
		System.out.println("Do you want to play again? Please renew it!");
		System.out.println("go go go");
	}
}

